﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Web;
using System.Collections;

namespace KLY.ServiceCore
{
    public static class BizCommon
    {

        public static string Domain
        {
            get
            {
                return System.Configuration.ConfigurationSettings.AppSettings["API_DOMAIN"].ToString();
            }
        }

        public static bool CheckIsValidateMobilePhone(string mobilePhone)
        {
            return Regex.IsMatch(mobilePhone, @"^[1]+\d{10}");
        }

        public static void SetLoginInfo(params string[] info)
        {
            List<string> loginInfo = new List<string>();
            foreach(var v in info)
            {
                loginInfo.Add(v);
            }
            SetUserLoginInfo(loginInfo);
        }

        public static string GetMemberTypeDesc(string memberType)
        {
            switch (memberType)
            {
                case "1":
                    return "货主";
                default:
                    return "货主";
            }
        }

        public static string GetAuthToken()
        {
            var loginInfo = BizCommon.GetLoginInfo();
            if (loginInfo != null)
            {
                return loginInfo[0];
            }
            return "";
        }

        public static List<string> GetLoginInfo()
        {
            var loginInfo = GetCookie("authToken");
            if (loginInfo != null && loginInfo.Any())
            {
                return loginInfo;
            }
            return null;
        }

        public static void DeleteLoginInfo()
        {
            DeleteUserCookie("authToken");
        }

        public static int PagedListMaxRecord
        {
            get
            {
                int maxRecord = 300;
                int.TryParse(System.Configuration.ConfigurationSettings.AppSettings["PagedListMaxRecord"], out maxRecord);
                return maxRecord;
            }
        }

        public static int PagedListPageSize
        {
            get
            {
                int pageSize = 10;
                int.TryParse(System.Configuration.ConfigurationSettings.AppSettings["PagedListPageSize"], out pageSize);
                return pageSize;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userCookieKey"></param>
        /// <param name="cookieValue"></param>
        private static void SetCookie(string userCookieKey, string cookieValue)
        {
            if (cookieValue == null)
            {
                return;
            }
            HttpCookie userCookie = HttpContext.Current.Request.Cookies[userCookieKey];
            if (userCookie == null)
            {
                userCookie = new HttpCookie(userCookieKey);
                userCookie.Value = cookieValue;
            }
            else
            {
                userCookie.Value = cookieValue;
            }
            //AddCookieDomain(userCookie);
            HttpContext.Current.Response.AppendCookie(userCookie);
        }

        private static List<string> GetCookie(string userCookieKey)
        {
            HttpCookie userCookie = HttpContext.Current.Request.Cookies[userCookieKey];
            if (userCookie == null)
            {
                return null;
            }
            if (!string.IsNullOrWhiteSpace(userCookie.Value))
            {
                return userCookie.Value.Split('|').ToList();
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userCookieKey"></param>
        private static void DeleteUserCookie(string userCookieKey)
        {
            HttpCookie userCookie = HttpContext.Current.Request.Cookies[userCookieKey];
            if (userCookie != null)
            {
                userCookie.Value = "";
                userCookie.Expires = DateTime.Now.AddDays(-1);
                //AddCookieDomain(userCookie);
                HttpContext.Current.Response.AppendCookie(userCookie);
            }
        }

        /// <summary>
        /// 添加cookie的domain
        /// </summary>
        /// <param name="userCookie"></param>
        private static void AddCookieDomain(HttpCookie userCookie)
        {
            userCookie.Domain = "quickly56.com";
            userCookie.Path = "/";
        }

        private static void SetUserLoginInfo(List<string> loginInfo)
        {
            if (loginInfo != null && loginInfo.Any())
            {
                string info = string.Empty;
                foreach (var v in loginInfo)
                {
                    info += v + "|";
                }
                SetCookie("authToken", info);
            }
        }
    }
}
